package hoi.birthdaymgr.utility;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;

/*
 * The editor button that brings up the dialog.
 */
//public class TablePopupEditor extends AbstractCellEditor
public class TablePopupEditor extends DefaultCellEditor implements TableCellEditor, ActionListener {

    private static final long serialVersionUID = 5251916563956317089L;

    private PopupDialog popup;
    private String currentText = "";
    private JButton editorComponent;

    public TablePopupEditor() {
        super(new JTextField());

        setClickCountToStart(2);

        // Use a JButton as the editor component

        editorComponent = new JButton();
        editorComponent.setBackground(Color.white);
        editorComponent.setBorderPainted(false);
        editorComponent.setContentAreaFilled(false);
        // editorComponent.addActionListener( this );

        // Set up the dialog where we do the actual editing

        popup = new PopupDialog();
    }

    public Object getCellEditorValue() {
        return currentText.replace("\r", "").replace("\n", " ");
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                popup.setText(currentText);
                popup.setLocationRelativeTo(editorComponent);
                popup.setVisible(true);
                fireEditingStopped();
            }
        });

        currentText = value.toString();
        editorComponent.setText(currentText);
        return editorComponent;
    }

    /*
     * When the editor button is clicked display the editing dialog
     */
    public void actionPerformed(ActionEvent e) {
        popup.setText(currentText);
        popup.setLocationRelativeTo(editorComponent);
        popup.setVisible(true);

        // Since dialog is modal editing will only stop when the
        // dialog is closed

        fireEditingStopped();
    }

    /*
     * Simple dialog containing the actual editing component
     */
    class PopupDialog extends JDialog implements ActionListener {
        private static final long serialVersionUID = -3993066787191641718L;

        private JTextArea textArea;

        public PopupDialog() {
            super((Frame) null, "编辑内容", true);

            textArea = new JTextArea(5, 20);
            textArea.setLineWrap(true);
            //textArea.setWrapStyleWord(true);
            KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
            textArea.getInputMap().put(keyStroke, "none");
            JScrollPane scrollPane = new JScrollPane(textArea);
            getContentPane().add(scrollPane);

            JButton cancel = new JButton("取消");
            cancel.addActionListener(this);
            JButton ok = new JButton("确定");
            ok.setPreferredSize(cancel.getPreferredSize());
            ok.addActionListener(this);

            JPanel buttons = new JPanel();
            buttons.add(ok);
            buttons.add(cancel);
            getContentPane().add(buttons, BorderLayout.SOUTH);
            pack();

            getRootPane().setDefaultButton(ok);
        }

        public void setText(String text) {
            textArea.setText(text);
        }

        /*
         * Save the changed text before hiding the popup
         */
        public void actionPerformed(ActionEvent e) {
            if ("确定".equals(e.getActionCommand())) {
                currentText = textArea.getText();
            }

            textArea.requestFocusInWindow();
            setVisible(false);
        }
    }

    public static void main(String[] args) {
        String[] columnNames = {
                "Item", "Description" };
        Object[][] data = {
                {
                        "Item 1", "Description of Item 1" }, {
                        "Item 2", "Description of Item 2" }, {
                        "Item 3", "Description of Item 3" } };

        JTable table = new JTable(data, columnNames);
        table.getColumnModel().getColumn(1).setPreferredWidth(300);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);

        // Use the popup editor on the second column

        TablePopupEditor popupEditor = new TablePopupEditor();
        table.getColumnModel().getColumn(1).setCellEditor(popupEditor);

        JFrame frame = new JFrame("Popup Editor Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
